package cn.com.generaldata.os.service.indicator_evaluation.incr_analysis;

import org.apache.commons.lang3.Validate;
import org.nutz.dao.sql.Criteria;

import cn.com.generaldata.jsme_indicator_system.entity.IndicatorEvaluation;
import cn.com.generaldata.jsme_indicator_system.util.Constants;
import cn.com.generaldata.os.service.indicator_constant.ProvinceIndicatorConstant;
import cn.com.generaldata.os.service.indicator_evaluation.IndicatorEvaluationCalculateService;
import cn.com.generaldata.os.vo.IndicatorAccountVO;

/**
 * 地区生产总值可比价评估指标计算类.
 */
public class GDPComparablePriceCalculateService extends IndicatorEvaluationCalculateService {
	/** 定期年份 */
	private int baseYear = 2001;
	/** 地区生产总值 */
	private String GDPComparablePriceIndicatorName = "地区生产总值可比价";

	private String GDPGrowthRateIndicatorName = "地区生产总值增长速度";

	@Override
	protected Double calculate(IndicatorEvaluation indicator, IndicatorAccountVO indicatorAccountVO) {
		Double result = null;
		if (indicator.getSection() == Constants.INDICATOR_LEVEL_PROVINCE) {
			result = calculateProvinceResult(indicator, indicatorAccountVO);
		} else if (indicator.getSection() == Constants.INDICATOR_LEVEL_CITY) {
			// 市指标没确定先保留
		}
		return result;
	}

	private Double calculateProvinceResult(IndicatorEvaluation indicator, IndicatorAccountVO indicatorAccountVO) {
		// 令基期2001年生产总值可比价=现价
		if (indicatorAccountVO.getYear() == baseYear) {
			return getGDP(indicatorAccountVO);
		}

		Double lastGDPComparablePrice = getLastGDPComparablePrice(indicatorAccountVO);

		Double r = getGDPGrowthRate(indicatorAccountVO);
		return lastGDPComparablePrice * (1 + r);
	}

	private Double getGDP(IndicatorAccountVO indicatorAccountVO) {
		Double result = getInputOrAccountingIndicatorValue(indicatorAccountVO, indicatorAccountVO.getYear(),
				ProvinceIndicatorConstant.GDP_INDICATOR_NAME);
		Validate.isTrue(result != null);
		return result;
	}

	private Double getLastGDPComparablePrice(IndicatorAccountVO indicatorAccountVO) {
		Integer indicatorId = getIndicatorEvaluationId(GDPComparablePriceIndicatorName, indicatorAccountVO.getAreaId());
		Criteria cri = getIndicatorDataCri(indicatorAccountVO, indicatorAccountVO.getYear() - 1, indicatorId);
		return getEvaluationIndicatorValue(cri);
	}

	private Double getGDPGrowthRate(IndicatorAccountVO indicatorAccountVO) {
		Double growthRate = getInputOrAccountingIndicatorValue(indicatorAccountVO, indicatorAccountVO.getYear(),
				GDPGrowthRateIndicatorName) / 100;
		Validate.isTrue(growthRate != null, "地区生产总值增长速度为0，不可以计算");
		return growthRate;
	}

}
